<?php
class FriendUtility
{
    public static function addFriends($userId, $infos)
    {
        // mysql
        $multi = new CDbMultiInsertCommand(FriendModel::model());
        $friendIds = array();
        foreach($infos as $info)
        {
            $friend = new FriendModel();
            $friend->user_id = $userId;
            $friend->friend_id = $friendIds[] = $info['uid'];
            $friend->friend_name = $info['name'];
            $friend->friend_avatar = $info['pic_big'];
            $multi->add($friend);
        }
        $multi->execute();

        // redis
        $friendlist = new FriendListKey($userId);
        call_user_func_array(array($friendlist, 'addMulti'), $friendIds);
    }

    public static function deleteByUserId($userId)
    {
        // mysql
        FriendModel::model()->deleteAllByAttributes(array('user_id' => $userId));
        
        // redis
        $friendlist = new FriendListKey($userId);
        $friendlist->delete();
    }

    public static function getMutualFriends($userId, $targetId)
    {
        $friendlist = new FriendListKey($userId);
        return $friendlist->getMutualFriends($targetId);
    }
}